#include "../../include/check.h"

/***************************************************
函数名:reg_check
功能:注册检查
传入参数:struct message *recievemsg
返回值:成功REG_OK，失败REG_FAULT
***************************************************/
int reg_check(struct message *recievemsg)
{
    int fd;
    int read_size,write_size;
    struct message cmpmsg;
	char *p_name;
	char *p_passwore;
	int rc,i,ncols;
	sqlite3 *db;
	sqlite3_stmt *stmt;
	char sql[128] ;
	const char *tail;
    if(strlen(recievemsg->name)>20 || strlen(recievemsg->msg)>20 )
    {
        return REG_FAULT;
    }
    if(my_strcmp(recievemsg->name,"admin")==0)
    {
        return REG_FAULT;
    }
    if(my_strcmp(recievemsg->name,"all")==0)
    {
        return REG_FAULT;
    }
    if(my_strcmp(recievemsg->name,"reg")==0)
    {
        return REG_FAULT;
    }
    if(my_strcmp(recievemsg->name,"login")==0)
    {
        return REG_FAULT;
    }
    if(my_strcmp(recievemsg->name,"trans")==0)
    {
        return REG_FAULT;
    }
	/*打开数据库*/
	rc = sqlite3_open("./src/db/chat.db",&db);
    if(rc)
    { 
         fprintf(stderr,"can't open databse:%s",sqlite3_errmsg(db));
         sqlite3_close(db);
         return REG_FAULT;
    }      
	/*检查重名退出*/
   	memset(sql,0,sizeof(sql));
	sprintf(sql,"select * from member where name='%s';",(recievemsg->name));
	rc = sqlite3_prepare(db,sql,strlen(sql),&stmt,&tail);
	if(rc != SQLITE_OK)
	{
			fprintf(stderr,"SQLerror:%s",sqlite3_errmsg(db));
	}
	  /*执行*/
	 rc = sqlite3_step(stmt);
	 ncols = sqlite3_column_count(stmt);
	 while (rc == SQLITE_ROW)
	 {
	     rc = sqlite3_step(stmt);

		 sqlite3_finalize(stmt);
		 sqlite3_close(db);
		 return REG_FAULT;
	 }
	  /*销毁stmt*/
	sqlite3_finalize(stmt);
    /*注册*/	
    memset(sql,0,sizeof(sql));
    sprintf(sql,"insert into member (name,password) values(?,?);");
	rc = sqlite3_prepare(db,sql,strlen(sql),&stmt,&tail);
	if(rc != SQLITE_OK)
	{
		fprintf(stderr,"SQLerror:%s",sqlite3_errmsg(db));
	}
	p_name = recievemsg->name;
	sqlite3_bind_text(stmt,1,p_name,strlen(p_name),NULL);
	p_passwore = recievemsg->msg;
	sqlite3_bind_text(stmt,2,p_passwore,strlen(p_passwore),NULL);  
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);

	  /*关闭数据库*/
	  sqlite3_close(db);
	  return REG_OK;
}


/***************************************************
函数名:login_check
功能:注册检查
传入参数:struct message *recievemsg
返回值:管理员admin成功LOG_ADMIN，
普通用户成功LOG_USER，失败LOG_FAULT
***************************************************/
int login_check(struct message *recievemsg)
{
	int fd;
 	int read_size,write_size;
    	struct message cmpmsg;
	char *p_name;
	char *p_passwore;
	int rc,i,ncols;
	sqlite3 *db;
	sqlite3_stmt *stmt;
	char sql[128] ;
	const char *tail;
	
	/*打开数据库*/
	rc = sqlite3_open("./src/db/chat.db",&db);     
    	if(rc)
    	{ 
        	 fprintf(stderr,"can't open databse:%s",sqlite3_errmsg(db));
         	sqlite3_close(db);
         	return LOG_FAULT;
    	}      
	/*检查用户名密码*/
	memset(sql,0,sizeof(sql));
	sprintf(sql,"select * from member where name='%s' and password='%s';",(recievemsg->name),(recievemsg->msg));
	rc = sqlite3_prepare(db,sql,strlen(sql),&stmt,&tail);
	if(rc != SQLITE_OK)
	{
			fprintf(stderr,"SQLerror:%s",sqlite3_errmsg(db));
	}
	  /*执行*/
	rc = sqlite3_step(stmt);
	ncols = sqlite3_column_count(stmt);
	while (rc == SQLITE_ROW)
	{
			 rc = sqlite3_step(stmt);
			 sqlite3_finalize(stmt);
			 sqlite3_close(db);
			 if(strcmp(recievemsg->name,"admin") == 0)
			 {
			     return LOG_ADMIN;
			 }
			 else
			 {
				 return LOG_USER;
			 }

	 }
	  /*销毁stmt*/
	sqlite3_finalize(stmt);
	  /*关闭数据库*/
	sqlite3_close(db);
	return LOG_FAULT;
}

